﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Jsptpd.Common.DB
{
    public class DBHelper
    {       
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Jsptpd.Common.DB.DBHelper));
        private static string connStr;
        private SqlConnection conn = null;


        public DBHelper()
        {
            initialConnectionString();           
        }       

        /// <summary>
        /// 执行非查询操作(执行增、删、改操作的)，不支持事务
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public int ExecuteNoneQuery(string sql, CommandType type, params SqlParameter[] pars)
        {
            int result = 0;
            conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = type;
            if (pars != null)
            {
                cmd.Parameters.AddRange(pars);
            }
            result = cmd.ExecuteNonQuery();
            //关闭连接对象
            conn.Close();
            return result;
        }    

        public static DataSet GetDS(string sql)
        {
            logger.Info("Begin to get dataset...");
            DataSet ds = null;
            SqlConnection sqlConn = new SqlConnection(connStr);
            try
            {
                sqlConn.Open();
                SqlDataAdapter ada = new SqlDataAdapter(sql, sqlConn);
                ds = new DataSet();
                ada.Fill(ds);                
            }
            catch (Exception e)
            {
                logger.Error("程序连接串为"+connStr+"|获取数据集异常，详细信息："+e.ToString());               
            }
            finally
            {
                sqlConn.Close();
            }            
            return ds;
        }

        public static Int32 executeNonQuery(string sql)
        {
            SqlConnection sqlconn = new SqlConnection(connStr);
            sqlconn.Open();
            SqlCommand sqlcomm = new SqlCommand(sql, sqlconn);
            try
            {
                return sqlcomm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                logger.Error("执行executeNonQuery发生错误" + e.ToString());
                return -1;
            }
            finally
            {
                sqlconn.Close();
            }
        }


        private void initialConnectionString()
        {
            try
            {
                connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
                if (string.IsNullOrEmpty(connStr))
                {
                    logger.Info("The connection string is null......");
                }
            }
            catch (Exception ex)
            {
                logger.Error("Get the database connection failed,the detail info:" + ex.ToString());
            }
        }
    }
}
